//
//  GenericCalculations.m
//  FuelLogFree
//
//  Created by Olaf on 8/24/10.
//  Copyright 2010 olafgreck. All rights reserved.
//

#import "GenericCalculations.h"
#import "Pref.h"


@implementation GenericCalculations

//@synthesize pumpIndex,
@synthesize gas;
//@synthesize  rawConsumedVolume, rawConsumption;


- (id) init { 
	if (self = [super init]) {   

	}
	return self;
}

-(id) initWithGas: (Gas *) currentGas{
	if (self = [super init]) { 
		self.gas = currentGas;
		NSDateFormatter * dateFormatter =  [FuelLogAppDelegate pref].dateFormatter;
		NSNumberFormatter * distanceFormatter =  [FuelLogAppDelegate pref].distanceFormatter;
		NSNumberFormatter * costFormatter	  =  [FuelLogAppDelegate pref].costFormatter;
		NSNumberFormatter * volumeFormatter   =  [FuelLogAppDelegate pref].volumeFormatter;
		NSString *fuelText = [FuelLogAppDelegate pref].fuelText;

		self.dateString = [dateFormatter stringFromDate:gas.date];
		self.costString = [[NSString alloc] initWithFormat:(NSLocalizedString(@"GasRow Cost",@"Cost: %@")), [costFormatter stringFromNumber:[NSNumber numberWithFloat:gas.cost]]];
		self.odometerString = [[NSString alloc] initWithFormat:(NSLocalizedString(@"GasRow Odometer: %@",@"Odometer: %@")), [distanceFormatter stringFromNumber:[NSNumber numberWithFloat:gas.odometer]]];
		self.fuelvolumeString = [[NSString alloc] initWithFormat:@"%@: %@", fuelText, [volumeFormatter stringFromNumber:[NSNumber numberWithFloat:gas.volume]]];


	}
	return self;
}



- (void)dealloc {
	[gas release];
	[dateString release];
	[odometerString release];
	[consumptionString release];
	[tripDistanceString release];
	[fuelvolumeString release];
	[costString release];
	[super dealloc];
}
#pragma mark -
- (NSString *)dateString {
    return [[dateString retain] autorelease];
}

- (void)setDateString:(NSString *)value {
    if (dateString != value) {
        [dateString release];
        dateString = [value copy];
    }
}

- (NSString *)odometerString {
    return [[odometerString retain] autorelease];
}

- (void)setOdometerString:(NSString *)value {
    if (odometerString != value) {
        [odometerString release];
        odometerString = [value copy];
    }
}

- (NSString *)consumptionString {
    return [[consumptionString retain] autorelease];
}

- (void)setConsumptionString:(float )value { 
	NSNumberFormatter *consumptionFormatter =  [FuelLogAppDelegate pref].consumptionFormatter;
	[consumptionString release];
	if (value != 0) {
	consumptionString = [[NSString stringWithFormat:@"%@ %@",
						 [consumptionFormatter stringFromNumber:[NSNumber numberWithFloat:value]],
						 [FuelLogAppDelegate pref].dpfText]
						 copy];
	}
	else {
		consumptionString = [[NSString stringWithFormat:@"- %@",
							  [FuelLogAppDelegate pref].dpfText]
							 copy];
	}
}

- (NSString *)tripDistanceString {
    return [[tripDistanceString retain] autorelease];
}

- (void)setTripDistanceString:(float )value {
	NSNumberFormatter *distanceFormatter = [FuelLogAppDelegate pref].distanceFormatter;
	if (value != 0) {
	tripDistanceString = [[NSString stringWithFormat:@"%@ %@",
						  [distanceFormatter stringFromNumber:[NSNumber numberWithFloat:value]],
						  [FuelLogAppDelegate pref].distanceText]
						  copy];
	}
	else {
		tripDistanceString = [[NSString stringWithFormat:@"- %@",
							   [FuelLogAppDelegate pref].distanceText]
							  copy];
	}

}

- (NSString *)fuelvolumeString {
    return [[fuelvolumeString retain] autorelease];
}

- (void)setFuelvolumeString:(NSString *)value {
    if (fuelvolumeString != value) {
        [fuelvolumeString release];
        fuelvolumeString = [value copy];
    }
}

- (NSString *)costString {
    return [[costString retain] autorelease];
}

- (void)setCostString:(NSString *)value {
    if (costString != value) {
        [costString release];
        costString = [value copy];
    }
}

#pragma mark -

-(BOOL)isConsumptionBetweenAvailable:(int)pumpIndex { // used in tableView to calc consumption for cell
	BOOL isConsumptionBetweenAvailable = FALSE;
	BOOL isTankAlwaysFullForIntervall = FALSE;
	FuelLogAppDelegate *appDelegate = (FuelLogAppDelegate *)[[UIApplication sharedApplication] delegate];
	int prefUnits = [FuelLogAppDelegate pref].units;
	float rawConsumedVolume = 0;
	[self setConsumptionString:0];
	[self setTripDistanceString:0];

	
	if (pumpIndex+1 < [appDelegate.pumps count]) {
		Gas * currentGas = (Gas *)[appDelegate.pumps objectAtIndex:pumpIndex] ;
		Gas * previousGas = (Gas *)[appDelegate.pumps objectAtIndex:pumpIndex+1] ;
		float drivenDistance  = currentGas.odometer- previousGas.odometer;
		[self setTripDistanceString:drivenDistance];
		rawConsumedVolume = currentGas.volume;	// get volume for current cell, current cell = pumpIndex
		if (currentGas.full && previousGas.full)  {
			isTankAlwaysFullForIntervall = TRUE;
			isConsumptionBetweenAvailable = TRUE;
		}
		if (isTankAlwaysFullForIntervall) {
			switch (prefUnits) {
				case ENGLISH:  
				case UK: 
					[self setConsumptionString:(drivenDistance/rawConsumedVolume)];
					break;
					break;
				case METRIC: 
					[self setConsumptionString:(drivenDistance/rawConsumedVolume)];
					break;
				case INV_METRIC:
					[self setConsumptionString:(rawConsumedVolume/( drivenDistance/100))];
					break;
				case NORWAY:
					[self setConsumptionString:(rawConsumedVolume/( drivenDistance/10))];
					break;
				default: break;
			}
//			[self setConsumptionString:(rawConsumedVolume/( drivenDistance/100))];
//			[self setTripDistanceString:drivenDistance];
			
		}
		else {
			[self setConsumptionString:0]; 
		}
	}
	return (isConsumptionBetweenAvailable && isTankAlwaysFullForIntervall);
	
}
#pragma mark -
-(BOOL)isConsumptionBetweenAvailable:(int)startIndex endIndex:(int)endIndex {
	BOOL isConsumptionBetweenAvailable = FALSE;
	BOOL isTankAlwaysFullForIntervall = TRUE;
	FuelLogAppDelegate *appDelegate = (FuelLogAppDelegate *)[[UIApplication sharedApplication] delegate];
	int prefUnits = [FuelLogAppDelegate pref].units;
	float rawConsumedVolume = 0;
//	float rawConsumption = 0;
	
	if ((startIndex >= 0) && (startIndex < endIndex) && (endIndex != [appDelegate.pumps count]))		{
		self.gas = (Gas *)[appDelegate.pumps objectAtIndex:endIndex];
		float startOdometer = gas.odometer;
		self.gas = (Gas *)[appDelegate.pumps objectAtIndex:startIndex];
		float endOdometer = gas.odometer;
		float drivenDistance = endOdometer - startOdometer;
		for(int i = startIndex; i <= endIndex; i++) {
			self.gas = (Gas *)[appDelegate.pumps objectAtIndex:i];
			if (gas.full) {
				//self.
				rawConsumedVolume += gas.volume;
				isConsumptionBetweenAvailable = TRUE;
			}
			else {
				isTankAlwaysFullForIntervall = FALSE;
				isConsumptionBetweenAvailable = FALSE;
				break;				
			}
		}
		if (isTankAlwaysFullForIntervall) {
			switch (prefUnits) {
				case ENGLISH: 
					break;
				case UK: break;
				case METRIC: 
					[self setConsumptionString:(rawConsumedVolume/( drivenDistance/100))];
					
					break;
				case INV_METRIC: break;
				case NORWAY: break;
				default: break;;
			}
			[self setConsumptionString:(rawConsumedVolume/( drivenDistance/100))];
			[self setTripDistanceString:drivenDistance];
			
		}
		else {
			[self setConsumptionString:0];
		}
		
		//	 debug(@"distance %f, self.rawConsumption %f, self.rawConsumedVolume %f", drivenDistance, self.rawConsumption, self.rawConsumedVolume);
	}
	return (isConsumptionBetweenAvailable && isTankAlwaysFullForIntervall);
}

-(BOOL)distanceBetween:(int)startIndex endIndex:(int)endIndex{
	bool returnValue = FALSE;
	return returnValue;
}


@end
